package br.com.edoc.gerentes;


import java.util.List;


import org.hibernate.Query;



import br.com.edoc.anotation.Consulta;


import br.com.edoc.entidade.Menu;
import br.com.edoc.entidade.MenuFolha;
import br.com.edoc.entidade.Modulo;
import br.com.edoc.entidade.Perfil;
import br.com.edoc.entidade.PermissaoUsuario;
import br.com.edoc.entidade.Usuario;


public class GerenteControleAcessoImpl extends GerentePadraoImpl implements GerenteControleAcesso{

	
	@Consulta
	public List<Menu> findByMenusRaiz(Modulo modulo){
		StringBuffer queryBuffer = new StringBuffer();
		queryBuffer.append("from Menu me where me.mother is null and me.modulo = :mod");

		Query query = getSession().createQuery(queryBuffer.toString());
		query.setEntity("mod", modulo);
		return listAndCast(query);
	}
	@Consulta
	public List<Menu> findByMenusFilhos(Menu menu){
		StringBuffer queryBuffer = new StringBuffer();
		queryBuffer.append("from Menu me where me.mother = :menu ");
		Query query = getSession().createQuery(queryBuffer.toString());
		query.setEntity("menu", menu);
		return listAndCast(query);
	}
	@Consulta
	public List<MenuFolha> findByMenuFolhaForModulo(MenuFolha menu){
		StringBuffer queryBuffer = new StringBuffer();
		queryBuffer.append("from MenuFolha mf where mf.modulo = :modulo and mf.descricao like :descricao ");
		Query query = getSession().createQuery(queryBuffer.toString());
		query.setEntity("modulo", menu.getModulo());
		query.setString("descricao", (menu.getDescricao()==null)?"%%":"%"+menu.getDescricao()+"%");
		return listAndCast(query);
	}
	@Consulta
	public Usuario logar(Usuario usuario){
		StringBuffer queryBuffer = new StringBuffer();
		queryBuffer.append("from Usuario us where us.login = :login and us.senha = :senha ");
		Query query = getSession().createQuery(queryBuffer.toString());
		query.setString("login", usuario.getLogin());
		query.setString("senha", usuario.getSenha());
		return (Usuario) query.uniqueResult();
	}

	@Consulta
	public List<PermissaoUsuario> findByPermissaoForUsuarioModulo(Usuario usuario,Modulo modulo){
		StringBuffer queryBuffer = new StringBuffer();
		queryBuffer.append(" select * from PERMISSAOUSUARIO  permi ");
		queryBuffer.append(" inner join PERFILUSUARIO perf on perf.pru_iddoobjeto = permi.pru_iddoobjeto ");
		queryBuffer.append(" inner join menu menu on menu.ME_IDDOOBJETO = permi.ME_IDDOOBJETO ");
		queryBuffer.append(" inner join MENUFUNCIONALIDADE fun on menu.ME_IDDOOBJETO = fun.ME_IDDOOBJETO  ");
		queryBuffer.append(" inner join USARIOPERFIL usp on  usp.PRU_IDDOOBJETO =perf.PRU_IDDOOBJETO  ");

		String valores = null;
		for(Perfil perfil : usuario.getListaPerfisUsuario()){
			if(perfil.getModulo().getId().equals(modulo.getId())){
				if(valores!=null){
					valores+=",";
				}else{
					valores="";
				}

				valores+=perfil.getId();
			}
		}
		queryBuffer.append(" where perf.pru_iddoobjeto in("+valores+") ");
		queryBuffer.append(" and usp.US_IDDOOBJETO ="+usuario.getId()+" order by menu.ME_DESCRICAO");
		Query query = getSession().createSQLQuery(queryBuffer.toString()).addEntity(PermissaoUsuario.class);

		return listAndCast(query);
	}

	@Consulta
	public List<Modulo> findModuloForUsuario(Usuario usuario){
		StringBuffer queryBuffer = new StringBuffer();
		queryBuffer.append(" select * from modulo mod ");
		queryBuffer.append(" inner join PERFILUSUARIO pfu on pfu.mod_iddoobjeto = mod.mod_iddoobjeto ");
		queryBuffer.append(" inner join USARIOPERFIL usp on  usp.PRU_IDDOOBJETO = pfu.PRU_IDDOOBJETO ");
		queryBuffer.append(" where usp.us_iddoobjeto = :idUsuario order by  mod.MOD_DESCRICAO");
		Query query = getSession().createSQLQuery(queryBuffer.toString()).addEntity(Modulo.class);
		query.setInteger("idUsuario", usuario.getId());
		return listAndCast(query);
	}


}
